package com.softa.web.controller.custom;

import com.github.pagehelper.PageInfo;
import com.softa.annotation.CurrentUser;
import com.softa.dao.dto.OrderDTO;
import com.softa.dao.dto.ProductDTO;
import com.softa.dao.model.SysUser;
import com.softa.service.business.OrderService;
import com.softa.service.business.ProductService;
import com.softa.service.business.ProductTypeService;
import com.softa.utils.Constants;
import com.softa.vo.DataGridRequest;
import com.softa.vo.DataGridResponse;
import com.softa.vo.PaginationResponse;
import com.softa.vo.QueryRequest;
import com.softa.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 产品信息
 * Created by yangl on 2017/8/11.
 */
@Controller
@RequestMapping("/custom/product")
public class CustomProductController extends BaseController{

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private OrderService orderService;

    /**
     * 产品列表
     */
    @RequestMapping("page/list")
    public String pageList(Model model) {
        model.addAttribute("types", productTypeService.queryAll());
        return Constants.MENU_FRONT+"/product_list";
    }

    /**
     * 产品详情
     */
    @RequestMapping("page/detail/{id}")
    public String pageDetail(Model model, @PathVariable("id")Long id) {
        model.addAttribute("product", productService.getProductDTOById(id));
        return Constants.MENU_FRONT+"/product_detail";
    }

    /**
     * 产品试用
     */
    @RequestMapping("page/try/{id}")
    public String pageTry(Model model, @PathVariable("id")Long id) {
        model.addAttribute("product", productService.getProductDTOById(id));
        return Constants.MENU_FRONT+"/product_try";
    }

    @RequestMapping(value="/data/list",method = RequestMethod.POST)
    @ResponseBody
    public PaginationResponse listPage(DataGridRequest request, QueryRequest queryRequest) {
        PageInfo<ProductDTO> pageResult = productService.queryProductPicByParam(request, queryRequest);
        return new PaginationResponse(pageResult, request.getRows());
    }

    @RequestMapping(value = "/data/trynum", method = RequestMethod.POST)
    @ResponseBody
    public int trynum(@CurrentUser SysUser loginUser, OrderDTO orderDTO) {
        int num = 0;
        try {
            orderDTO.setPurId(loginUser.getId());
            num = orderService.getTryNumByUserAndProduct(orderDTO);
        } catch (Exception e) {
            num = 100;
            logger.error(e.getMessage());
        }
        return num;
    }

    @RequestMapping(value = "/data/createorder", method = RequestMethod.POST)
    @ResponseBody
    public String dataTry(@CurrentUser SysUser loginUser, OrderDTO orderDTO) {
        String message="提交成功";
        try {
            orderDTO.setCreateUser(loginUser.getId());
            orderDTO.setUpdateUser(loginUser.getId());
            orderDTO.setPurId(loginUser.getId());
            message = orderService.createOrder(orderDTO);
        } catch (Exception e) {
            message="提交失败";
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return message;
    }

}
